/**
 * Created with JetBrains WebStorm.
 * User: kongxuelei
 * Date: 14-3-29
 * Time: 下午6:07
 * To change this template use File | Settings | File Templates.
 */
$(function(){

    CHTouch.widget={
        SNCarousel: function (c) {
            var b = this;
            var d = {
                hook: "#J_index_slide",
                direction: "X",
                slideBox: ".index-slide-box",
                slideUl: ".slide_ul",
                slideLi: ".slide_ul li",
                prev: ".prev",
                next: ".next",
                disable: "disabled",
                counter: ".trigger",
                effect: "scroll",
                current: 1,
                timer: 350,
                autoplay: 1,
                cycle: 1,
                touch: true,
                asyncLoad: true
            };
            if (c) {
                $.extend(d, c)
            }
            var slideWidth= document.body.clientWidth;
            $(d.slideBox).width(slideWidth);
            $(d.slideLi).width(slideWidth);
            var scale=slideWidth/720;
            $(d.hook).height(Math.round(312*scale));
//            if (!b.D(d.hook)) {
//                return
//            }
            function e(s) {
                var Q = s;
                var A = $(Q.hook);
                var n = Q.hook;
                var j = Q.current;
                var t = 0,
                    M = 0,
                    D = 0,
                    z = 0,
                    L = $(Q.slideLi).length,
                    B = $(Q.slideLi),
                    C = $(Q.slideUl),
                    v = $(B),
                    x = $(C),
                    D = A.find(Q.slideBox).width(),
                    z = A.find(Q.slideBox).height(),
                    g = Q.direction == "X" ? true : false,
                    u = Q.autoplay,
                    J = false,
                    m = null;
                if (L == 1) {
                    C[0].onmousedown = C[0].ontouchstart = null;
                    P();
                    return
                }
                /*生成轮播按钮*/
                for(var i=0;i<L;i++){
                    document.querySelector(Q.counter).appendChild( document.createElement("li"))
                }
                if (Q.cycle == 1) {
                    var f = $(B[L - 1]).clone();
                    var O = $(B[0]).clone();
                    x.append(O);
                    x.append(f);
                    C[0].style.width = D * (L + 2) + "px";
                    f.get(0).style.left = -D * (L + 2) + "px"
                }
                if (g) {
                    for (var I = 0; I < L; I++) {
                        t += v.eq(I).width()
                    }
                } else {
                    for (var I = 0; I < L; I++) {
                        M += v.eq(I).height()
                    }
                }
                P();
                switch (Q.effect) {
                    case "ctrl":
                        o();
                        break;
                    case "scroll":
                        R();
                        break;
                    case "both":
                        o();
                        R()
                }

                function R() {
                    if (g) {
                        var h = -t + D
                    } else {
                        var h = -M + z
                    } if (Q.touch) {
                        C[0].onmousedown = C[0].ontouchstart = l
                    } else {
                        C.hover(function () {
                            k.dispose()
                        }, function () {
                            k.process()
                        })
                    }

                    function l(r) {
                        var w, T;
                        k.dispose();
                        var U = [x.position().left, x.position().top];
                        w = p(r);
                        C[0].ontouchmove = C[0].onmousemove = S;

                        function S(W) {
                            T = p(W);
                            if (g) {
                                var V = (T[0] - w[0]) + U[0];
                                if (Math.abs(T[0] - w[0]) - Math.abs(T[1] - w[1]) > 0) {
                                    W.preventDefault();
                                    X();
                                    C[0].ontouchend = document.onmouseup = N
                                }
                            } else {
                                var V = (T[1] - w[1]) + U[1];
                                W.preventDefault();
                                X();
                                C[0].ontouchend = document.onmouseup = N
                            }

                            function X() {
                                if (g) {
                                    C[0].style.left = V + "px"
                                } else {
                                    C[0].style.top = V + "px"
                                }
                            }
                            if (J) {
                                C[0].ontouchmove = C[0].ontouchend = C[0].onmousemove = document.onmouseup = null
                            }
                        }

                        function N(W) {
                            var X, V = p(W);
                            if (u) {
                                k.process()
                            }
                            if (g) {
                                X = V[0] - w[0];
                                Y(W)
                            } else {
                                X = V[1] - w[1];
                                Y(W)
                            }

                            function Y() {
                                if (X < -D / 6) {
                                    G()
                                } else {
                                    if (X < 0 && X > -D / 5) {
                                        H()
                                    } else {
                                        if (X > D / 5) {
                                            K()
                                        } else {
                                            H()
                                        }
                                    }
                                }
                            }
                            P();
                            C[0].ontouchmove = C[0].ontouchend = C[0].onmousemove = document.onmouseup = null
                        }
                    }

                    function p(r) {
                        var w = [];
                        w[0] = r.changedTouches ? r.changedTouches[0].clientX : r.clientX;
                        w[1] = r.changedTouches ? r.changedTouches[0].clientY : r.clientY;
                        return w
                    }
                }
                var H = function () {
                    x.animate({
                        left: -(D * (j - 1))
                    });
                    if (Q.cycle == 1) {} else {}
                };
                var K = function () {
                    if (u) {
                        k.process()
                    }
                    if (Q.cycle == 1) {
                        if (j != 1) {
                            y();
                            return false
                        } else {
                            y();
                            B[L - 1].style.left = -(D * L) + "px";
                            B[0].style.left = 0;
                            return false
                        }
                    } else {
                        if (j != 1) {
                            y();
                            return false
                        } else {
                            H()
                        }
                    }
                };

                function y() {
                    if (J) {
                        return
                    }
                    if (g) {
                        A.find(Q.slideUl).animate({
                            left: -(D * (j - 2))
                        }, Q.timer, function () {
                            P();
                            C[0].style.left = -(D * (j - 1)) + "px";
                            B[L - 1].style.left = 0;
                            J = false
                        })
                    } else {
                        x.animate({
                            top: -(z * (j - 2))
                        }, Q.timer, function () {
                            J = false
                        })
                    }
                    j == 1 ? j = L : j--;
                    J = true;
                    if (!Q.touch || Q.asyncLoad) {
                        q()
                    }
                }
                var G = function () {
                    if (Q.autoplay == 1) {
                        k.process()
                    }
                    if (Q.cycle == 1) {
                        if (j != L) {
                            F();
                            return false
                        } else {
                            F();
                            B[0].style.left = D * L + "px";
                            return false
                        }
                    } else {
                        if (j != L) {
                            F();
                            return false
                        } else {
                            H()
                        }
                    }
                };

                function F() {
                    if (J) {
                        return
                    }
                    if (Q.direction == "X") {
                        A.find(Q.slideUl).animate({
                            left: -(D * j)
                        }, Q.timer, function () {
                            P();
                            C[0].style.left = -(D * (j - 1));
                            B[0].style.left = 0;
                            J = false
                        })
                    } else {
                        x.animate({
                            top: -(z * j)
                        }, Q.timer, function () {
                            J = false
                        })
                    }
                    J = true;
                    j == L ? j = 1 : j++;
                    if (!Q.touch || Q.asyncLoad) {
                        q()
                    }
                }
                var q = function () {
                    try {
                        v.eq(j - 1).find("img").attr("src", v.eq(j - 1).find("img").attr("data-src")).removeAttr("data-src")
                    } catch (h) {}
                };
                var E = {
                    timerid: null,
                    action: function (h) {
                        try {
                            h()
                        } catch (l) {}
                    },
                    process: function (h) {
                        clearTimeout(this.timerid);
                        this.timerid = setTimeout(function () {
                            E.action(h)
                        }, 0)
                    }
                };

                function o() {
                    A.find(Q.prev).click(function (h) {
                        if (Q.cycle == 1) {
                            E.process(K)
                        } else {
                            if (j != 1) {
                                E.process(K)
                            }
                        }
                    });
                    A.find(Q.next).click(function (h) {
                        if (Q.cycle == 1) {
                            E.process(G)
                        } else {
                            if (j != L) {
                                E.process(G)
                            }
                        }
                    })
                }

                function P() {
                    var l = A.find(Q.counter);
                    if (l.length > 0) {
                        l.find("li").eq(j - 1).addClass("cur").siblings().removeClass("cur")
                    } else {
                        if (Q.cycle != 1) {
                            var p = A.find(Q.prev),
                                h = A.find(Q.next);
                            p.removeClass(Q.disable);
                            h.removeClass(Q.disable);
                            if (L == 1) {
                                p.addClass(Q.disable);
                                h.addClass(Q.disable);
                                return
                            }
                            if (j == 1) {
                                p.addClass(Q.disable)
                            } else {
                                if (j == L) {
                                    h.addClass(Q.disable)
                                }
                            }
                        }
                    }
                }
                var k = {
                    timeoutId: null,
                    performProcessing: function () {
                        try {
                            G()
                        } catch (h) {}
                    },
                    process: function () {
                        clearInterval(this.timeoutId);
                        this.timeoutId = setInterval(function () {
                            k.performProcessing()
                        }, 5000)
                    },
                    dispose: function () {
                        clearInterval(this.timeoutId);
                        return
                    }
                };
                if (Q.autoplay == 1) {
                    k.process()
                }
            }
            return e(d)
        }
    };
    CHTouch.widget.SNCarousel();

});
